#include "HufArc.h"

__HufTree ** SortHuffTree (__HufTree ** H_T)
{
	__HufTree * elm = NULL;
	__HufTree ** huffTree_new = NULL;
	int i = 0;
	int m = 0;
	int tmp = 0;
	int count = 1;
	while (i <= 255)
	{
		m = 0;
		tmp = 0;
		elm = H_T[0];
		while ( m <= 255)
		{
			if (H_T[m])
				if (elm)
					if (elm->count > H_T[m]->count)
					{
						elm = H_T[m];
						tmp = m;
					}
					else {}
				else {
					elm = H_T[m];
					tmp = m;
				}
				m++;
		}
		H_T[tmp] = NULL;
		if (elm)
		{
			huffTree_new = (__HufTree **) realloc (huffTree_new,count*sizeof(__HufTree*));
			if (!huffTree_new)
				cl_exit (HUF_TREE_INIT, MEMORY_ERR);
			huffTree_new[count - 1] = elm;
			count ++;
			//free (elm);
			elm = NULL;

		}
		i++;
	}
	huffTree_new = (__HufTree **) realloc (huffTree_new,count*sizeof(__HufTree*));
	if (!huffTree_new)
		cl_exit (HUF_TREE_INIT, MEMORY_ERR);
	huffTree_new[count - 1] = NULL;
	m = 0;
	return huffTree_new;
}